<?php
/* 扫呗支付 */
namespace app\common\model;
use GuzzleHttp\Client;
use think\Model;
class SbPay extends Model
{
  public static $api = 'http://test.lcsw.cn:8045/lcsw/';//接口地址
  public static $key;//令牌
  public static $merchant_no;//商户号
  public static $terminal_id;//终端号
  public static $accessToken;
  public static $client;

  public  function __construct () {
    self::$key = 'b9930b05ce4f488db4108c7eb7c83862';
    self::$merchant_no = '885200090000038';
    self::$terminal_id = '30057414';
    self::$client = new Client(['base_uri' => self::$api]);
  }


  /* 统一下单 */
  public  function jspay ($config) {
    $params = [
      'pay_ver' => '100',
      'pay_type' => '010',
      'service_id' => '012',
      'merchant_no' => self::$merchant_no,
      'terminal_id' => self::$terminal_id,
      'terminal_trace' => $config['out_trade_no'],
      'terminal_time' => date('YmdHis'),
      'total_fee' => $config['total_fee'],
      'sub_appid' => 'wx639023a1ded5cf4b',
      'open_id' => $config['openid'],
      'order_body' => $config['body'],
      'notify_url' => $config['notify_url'],
      //'attach' => '',
      //'goods_detail' => '',
    ];
    $signArr = $params;
    unset($signArr['sub_appid']);
    unset($signArr['open_id']);
    unset($signArr['order_body']);
    unset($signArr['notify_url']);
    $params['key_sign'] = self::getSign($signArr);
    $response = self::$client->request('POST', 'pay/100/jspay', [
      'json' => $params
    ]);
    pr($params);
    $body = $response->getBody();
    $stringBody = (string) $body;
    pr($stringBody);
  }

  /* 签名 */
  public function getSign ($params) {
    $params['access_token'] = self::$key;
    pr($params);
    $string = http_build_query($params);
    return md5($string);
  }

}